<?php 
require_once('mysql.php');
/* class Client
 *
 * description:
 * to store the basic data of a client like user name
 * and to handle clients action
 *
 *
 * 
 * since version: 0.2
 */
class Client{
  
  /* var $username,$mailbox,$realname,$pwdmd5
   *
   * type:string
   *
   * description:
   * to store the basic information of a client
   *
   *
   * since version:0.2
   */
  var $username;
  var $mailbox;
  var $realname;
  var $pwdmd5;

  /**
   *  function: get_client_by_name($username)
   *
   *  description: return a client instance by name
   *
   *  param string $username
   *
   *  return client instance|null
   *
   *  @since version: 0.2 
   */
  static function get_client_by_name($username)
  {
    if(!isset($username))
      {
	return null;
      }
    if(!MySQL::ckstr($username))
      {
	return null;
      }
    $con = MySQL::get_con();
    if(!$con)
      {
	return null;
      }
    $result = mysql_query("SELECT * FROM client WHERE username = '$username'",$con);
    if(!$result||mysql_affected_rows()!=1)
      {
	return null;
      }
    $row = mysql_fetch_array($result);
    if(!$row)
      {
	return null;
      }
    $client = new Client($row['username'],$row['mailbox'],$row['realname'],$row['password']);
    if(!$client->is_valid())
      return null;
    return $client;
  }
  
  /**
   *  function: static function get_current_checkout($username)
   *
   *  description: to return the checkout information of a user
   *
   *  param string $username
   *
   *  return array(index = > array(name/*bookname/,ID,checkout_time));
   *
   *  @since version: 0.2 
   */
  static function get_current_checkout($username)
  {
    
    if(!isset($username))
      {
	return null;
      }
    if(!MySQL::ckstr($username))
      {
	return null;
      }
    $con = MySQL::get_con();
    if(!$con)
      {
	return null;	
      }
    $result = mysql_query("SELECT * FROM checkout_record WHERE username = '$username' and is_returned = false",$con);
    $currents = array();
    while($row = mysql_fetch_array($result))
      {
	$currents[] = array('name'=>$row['name'],'checkout_time'=>$row['checkout_time'],'ID'=>$row['ID']);
      }
    return $currents;
  }
  
  /**
   *  function: static function get_checkout_history($username)
   *
   *  description: return the history of checkout books of a 
   *  certain user
   *
   *  param string $username
   *
   *  return array(name,ID,checkout_time,return_time)
   *
   *  @since version: 0.2 
   */
  static function get_checkout_history($username)
  {
    if(!isset($username))
      {
	return null;
      }
    if(!MySQL::ckstr($username))
      {
	return null;
      }
    $con = MySQL::get_con();
    if(!$con)
      {
	return null;
      }
    $result = mysql_query("SELECT * FROM checkout_record WHERE username = '$username' and is_returned = true",$con);
    if(!$result)
      {
	return null;
      }
    $history = array();
    while($row = mysql_fetch_array($result))
      {
	$history[] = array('name'=>$row['name'],'checkout_time'=>$row['checkout_time'],'return_time'=>$row['return_time'],'ID'=>$row['ID']);
      }
    return $history;
  }
  
  /**
   *  function: static function del_client_by_username($username)
   *
   *  description: like title
   *
   *  param string $username
   *
   *  return boolean
   *
   *  @since version: 0.2 
   */
  static function del_client_by_username($username)
  {
    if(!isset($username))
      {
	return false;
      }
    if(!MySQL::ckstr($username))
      {
	return false;
      }
    $con = MySQL::get_con();
    if(!$con)
      {
	return false;
      }
    $result = mysql_query("DELETE FROM client WHERE username = '$username'",$con);
    if(!$result)
      {
	return false;
      }
    if (mysql_affected_rows()!=1)
      {
	return false;
      }
    return true;
  }
  
  /**
   *  function: function Client
   *
   *  description: default constructor
   *
   *  param string $username,$mailbox,$realname,$password
   *
   *  return type_desc
   *
   *  @since version: current_version 
   */
  function Client($username,$mailbox,$realname,$password)
  {
    $this->username = $username;
    $this->mailbox = $mailbox;
    $this->realname = $realname;
    $this->pwdmd5 = $password;
  }
  /**
   *  function: function is_valid()
   *
   *  description: to check if this instance is avalid user
   *
   *  param void
   *
   *  return boolean
   *
   *  @since version: 0.2 
   */

  function is_valid()
  {
    if(!isset($this->username,$this->mailbox,$this->realname,$this->pwdmd5))
      {
	return false;
      }
    if(!MySQL::ckstr($this->username)||
       !MySQL::ckstr($this->mailbox)||
       !MySQL::ckstr($this->realname)||
       !MySQL::ckstr($this->pwdmd5))
      {
	return false;
      }
    if(strlen($this->pwdmd5)<32)
      {
	return false;
      }
    return true;
  }
    
  /**
   *  function: function save_this()
   *
   *  description: save this client to MySQL
   *
   *  param void
   *
   *  return boolean
   *
   *  @since version: 0.2 
   */
  function save_this()
  {
    if(!$this->is_valid())
      return false;
    $con = MySQL::get_con();
    if(!$con)
      {
	return false;
      }
    $result = mysql_query("INSERT INTO client SET username = '$this->username' , mailbox = '$this->mailbox' , realname = '$this->realname' , password = '$this->pwdmd5'",$con);
    if(!$result || mysql_affected_rows()!=1)
      {
	return false;
      }
    return true;
  }

  /**
   *  function: function update_this()
   *
   *  description: update this client information
   *
   *  param void
   *
   *  return boolean
   *
   *  @since version: 0.2 
   */
  function update_this()
  {
    if(!$this->is_valid())
      {
	return false;
      }
    $con = MySQL::get_con();
    if(!$con)
      {
	return false;
      }
    $result = mysql_query("UPDATE client SET username = '$this->username' ,mailbox = '$this->mailbox' , realname = '$this->realname',  password = '$this->pwdmd5' WHERE username = '$this->username'" ,$con);
    if(!$result||mysql_affected_row()!=1)
      {
	return false;
      }
    return true;
  }
}

?>